package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import ysq.xyg.dao.ActivityDao;
import ysq.xyg.model.Activity;
import ysq.xyg.service.ActivityService;
import ysq.xyg.util.BaseConstant;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 商品活动
 *
 * @author congcong tao
 * @create 2018/8/8 下午9:34
 */
@Service("activityService")
public class ActivityServiceImpl extends BaseServiceImpl<Activity> implements ActivityService {

    @Resource
    private ActivityDao activityDao;
    @Autowired
    public void setActivityDao(ActivityDao activityDao) {
        this.baseDao = activityDao;
    }

    @Override
    public PageInfo<Activity> pageInfo(Activity activity, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Activity> list = activityDao.selectEntityOrder(activity);
        return new PageInfo<Activity>(list);
    }

    @Override
    public int selectMaxPeriods(String itemId) {
        return activityDao.selectMaxPeriods(itemId);
    }

    @Override
    public boolean autoActivity(Activity activity) {
        if (StringUtils.equals(activity.getIsAuto(), BaseConstant.ITEM_ACT_AUTO_NO)) {
            return false;
        }
        Activity newActivity = new Activity();
        newActivity.setId(BaseUtil.uId());
        newActivity.setItemId(activity.getItemId());
        newActivity.setPeriodsNum(activity.getPeriodsNum() + 1);
        newActivity.setTotalNum(activity.getTotalNum());
        newActivity.setIsAuto(activity.getIsAuto());
        newActivity.setTitle("第" + newActivity.getPeriodsNum() + "期");
        newActivity.setCreateTime(new Date());
        int id = this.insert(newActivity);
        if (id <= 0) {
            return false;
        }
        return true;
    }

    @Override
    public PageInfo<Activity> pageInfoOrder(Activity activity, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Activity> list = activityDao.selectEntityOrder(activity);
        return new PageInfo<Activity>(list);
    }

    @Override
    public Activity selectEntityById(String activityId) {
        Activity record = new Activity();
        record.setId(activityId);
        List<Activity> list = activityDao.selectEntityOrder(record);
        if (list == null || list.size() == 0) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public PageInfo<Activity> pagePastActInfo(Activity activity, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Activity> list = activityDao.selectPastActList(activity);
        return new PageInfo<Activity>(list);
    }

    @Override
    public List<Activity> selectEntityByMemberId(String memberId, String status) {
        return activityDao.selectEntityByMemberId(memberId, status);
    }

    @Override
    public List<Activity> selectEntityLimit(String itemId, int pageStart, int pageEnd) {
        return activityDao.selectEntityLimit(itemId, pageStart, pageEnd);
    }
}
